<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Job_preference extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->check_authentication(1);
		$this->load->model('Candidate_job_preference_model', 'job_preference');
		$this->lang->load('candidate/job_preference', $this->session->userdata['user_lang']);
	}
	
	public function index()
	{
		$job_preference_list = $this->job_preference->get(array('idcandidate' => $this->session->userdata['idprofile']));
				
		$grid_job_preference = array();
        
        foreach ($job_preference_list->result() as $row)
		{
			$array_hierarchy = array();
			$job_hierarchy_list = $this->job_preference->get_job_hierarchy(array('idjob' => $row->id));
			foreach ($job_hierarchy_list->result() as $line)
			{
				$array_hierarchy[] = $line->hierarchy;
			}
			$hierarchy_text = $array_hierarchy ? ('<b>'.$this->lang->line('job_preference_hierarchy') . '</b><br/>' . implode(', ', $array_hierarchy)) : $this->lang->line('job_preference_anyhierarchy');
			
			$array_specialty = array();
			$job_specialty_list = $this->job_preference->get_job_specialty(array('idjob' => $row->id));
			foreach ($job_specialty_list->result() as $line)
			{
				$array_specialty[] = $line->specialty;
			}
			$specialty_text = $array_specialty ? ('<b>'.$this->lang->line('job_preference_specialty') . '</b><br/>' . implode(', ', $array_specialty)) : $this->lang->line('job_preference_anyspecialty');
			
			$array_state = array();
			$job_state_list = $this->job_preference->get_job_state(array('idjob' => $row->id));
			foreach ($job_state_list->result() as $line)
			{
				$array_state[] = $line->state;
			}
			$state_text = $array_state ? ('<b>'.$this->lang->line('job_preference_state') . '</b><br/>' . implode(', ', $array_state)) : $this->lang->line('job_preference_anystate');
			
			$country_text = $row->country ? '<b>'.$this->lang->line('job_preference_country') . '</b><br/>' . $row->country : $this->lang->line('job_preference_anycountry');
			
			$salary_text = '<b>'.$this->lang->line('job_preference_salary') . '</b><br/>' . $row->minsalary . ' '.$this->lang->line('job_preference_salaryto').' ' . $row->maxsalary . ' ' .$this->lang->line('job_preference_currency');
			
			$grid_job_preference[] = '<div class="row"><span><span class="gtitle">' . $row->category .'</span>'
			. '<br/><span class="gdesc"><br/>'
			. $specialty_text.'<br/>'
			. $hierarchy_text.'<br/>'
			. $salary_text.'<br/>'
			. $country_text.'<br/>'
			. $state_text.'<br/>
			</span></span>
			<div class="gtool"><a onclick="CandidateJobPreference.editJobPreference('.$row->id.');">'.$this->lang->line('global_update').'</a>
			 &middot; <a onclick="CandidateJobPreference.deleteJobPreference('.$row->id.');">'.$this->lang->line('global_delete').'</a></div></div>';
		}
		
		if (!$grid_job_preference) {
			 $data['grid_job_preference'] = '<center>'.$this->lang->line('job_preference_noresults').'</center>';
		} else {
			 $data['grid_job_preference'] = implode('<hr/>', $grid_job_preference);
		}
		
		exitHTML($data['grid_job_preference']);
	}
	
	public function insert()
	{
		$data = $_POST;
		
		if (missing_field($data, array('idcategory')))
		{
			formError($this->lang->line('job_preference_missingfield'), true);
		}
		
		$data['idcandidate'] = $this->session->userdata['idprofile'];
		
		if ($this->job_preference->get(array('idcategory' => $data['idcategory'], 'idcandidate' => $this->session->userdata['idprofile']))->row()) {
			formError($this->lang->line('job_preference_categoryexists'), true);
		}
		
		if ($this->job_preference->insert($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidateJobPreference.getJobPreferenceGrid();closeBox();');
		}
	}
	
	public function update($id_job_preference = '')
	{
		$data = $_POST;
		
		if (missing_field($data, array('idcategory')))
		{
			formError($this->lang->line('job_preference_missingfield'), true);
		}
		
		$data['idcandidate'] = $this->session->userdata['idprofile'];
		
		$row = $this->job_preference->get(array('idcategory' => $data['idcategory'], 'idcandidate' => $this->session->userdata['idprofile']))->row();
		
		if ($row && $row->id != $data['idjobpreference']) {
			formError($this->lang->line('job_preference_categoryexists'), true);
		}
		
		if ($this->job_preference->update($data) === false)
		{
		   formError($this->lang->line('global_systemfail'), true);
		}
		else
		{
			exitJs('CandidateJobPreference.getJobPreferenceGrid();closeBox();');
		}
	}
	
	public function delete()
	{
		$data = $_POST;
		
		if (missing_field($data, array('id')))
		{
			formError($this->lang->line('job_preference_missingfield'));
		}
		
		if ($this->job_preference->delete(array('id' => $data['id'])) === false)
		{
		   formError($this->lang->line('global_systemfail'));
		}
		else
		{
			exitJs('CandidateJobPreference.getJobPreferenceGrid();');
		}
	}
	
	public function edit($id_job_preference = '')
	{
		header("Content-type: text/html; charset=utf-8");
		$data = $_POST;
		$data['id_jobpreference'] = $id_job_preference;
		$data['minsalary'] = '';
		$data['maxsalary'] = '';
		
		if (is_numeric($id_job_preference))
		{
			$job_preference_data = $this->job_preference->get(array('idjobpreference' => $id_job_preference))->row();
			
			$data['minsalary'] = $job_preference_data->minsalary;
			$data['maxsalary'] = $job_preference_data->maxsalary;
			$data['set_category_value'] = '$("#idcategory").val("'.$job_preference_data->idcategory.'");';
			$data['extrascript'] = '';
			
			$array_hierarchy = array();
			$job_hierarchy_list = $this->job_preference->get_job_hierarchy(array('idjob' => $job_preference_data->id));
			foreach ($job_hierarchy_list->result() as $line)
			{
				$array_hierarchy[] = $line->idhierarchy;
			}
			$hierarchy_script = $array_hierarchy ? '["' . implode('", "', $array_hierarchy) . '"]' : '[""]';
			
			$array_specialty = array();
			$job_specialty_list = $this->job_preference->get_job_specialty(array('idjob' => $job_preference_data->id));
			foreach ($job_specialty_list->result() as $line)
			{
				$array_specialty[] = $line->idspecialty;
			}
			$specialty_script = $array_specialty ? '["' . implode('", "', $array_specialty) . '"]' : '[""]';
			
			$array_state = array();
			$job_state_list = $this->job_preference->get_job_state(array('idjob' => $job_preference_data->id));
			foreach ($job_state_list->result() as $line)
			{
				$array_state[] = $line->idstate;
			}
			$state_script = $array_state ? '["' . implode('", "', $array_state) . '"]' : '[""]';
			
			
			$data['extrascript'] .= '$("#idhierarchy").val('.$hierarchy_script.');';
			$data['extrascript'] .= '$("#idspecialty").val('.$specialty_script.');';
			$data['set_country_value'] = '$("#idcountry").val("'.$job_preference_data->idcountry.'");';
			$data['extrascript'] .= '$("#idstate").val('.$state_script.');';
		}
		
		$data['userdata'] = $this->session->userdata;
        $this->load->view('candidate/job_preference', $data);
	}
	
	public function add()
	{
		$this->edit();
	}
}

?>